<% content_for :head do %>
  <%= stylesheet_link_tag :datepicker %>
  <%= javascript_include_tag :datepicker %>
  <%= javascript_include_tag "/assets/ckeditor/ckeditor.js" %>
<% end %>

<%= form_for([:admin, @product], :validate => true) do |f| %>

<%= render "shared/error_messages", :target => @product %>

<%= f.hidden_field :is_master, :value => "1" %>

<table>
  <tbody>
    <tr>
      <th><%= f.label :presentation %></th>
      <td colspan="3"><%= f.text_field :presentation, :maxlength => 30, :size => 40, :onkeyup => "getElementById('product_url').value = make_url(this.value);" %></td>
    </tr>
    <tr>
      <th><%= f.label :url %></th>
      <td colspan="3"><%= f.text_field :url, :maxlength => 70, :size => 40 %></td>
    </tr>
    <tr>
      <th><%= f.label :title %></th>
      <td colspan="3"><%= f.text_field :title, :maxlength => 250, :size => 80 %></td>
    </tr>
    <tr>
      <th><%= f.label :number %></th>
      <td colspan="3"><%= f.text_field :number, :maxlength => 70, :size => 40 %></td>
    </tr>
    <tr>
      <th><%= f.label :price %></th>
      <td><%= f.text_field :price, :size => 10, :maxlength => 12, :onfocus => "if(this.value == 0.00) this.value='';" %>&nbsp;Kč,&nbsp;za&nbsp;
      <%= AmountType.all.blank? ? raw("<span style='color: red;'>Nejdříve vytvořte typ množství</span>") : (f.collection_select :amount_type_id, AmountType.all, :id, :name) %></td>
      <th><%= f.label :tax_category %></th>
      <td><%= TaxCategory.all.blank? ? raw("<span style='color: red;'>Nejdříve vytvořte daňovou skupinu</span>") : (f.select :tax_category_id, TaxCategory.all.collect {|tax| ["#{tax.presentation} - #{tax.tax}%", tax.id]}) %></td>
    </tr>
    <tr>
      <th><%= f.label :state %></th>
      <td>
        <% if f.object.deleted? %>
          <%= button_to "Obnovit výrobek &raquo;", admin_revive_product_path(f.object), :class => "submit_button", :method => :get %>
        <% else %>
          <label><%= f.radio_button :state, "on_sale" %>&nbsp;<%= t 'enumerations.product.state.on_sale' %></label>
          <label><%= f.radio_button :state, "not_on_sale" %>&nbsp;<%= t 'enumerations.product.state.not_on_sale' %></label>
          <label><%= f.radio_button :state, "hidden" %>&nbsp;<%= t 'enumerations.product.state.hidden' %></label>
        <% end %>
      </td>
      <th><%= f.label :minimum_quantity %></th>
      <td><%= f.text_field :minimum_quantity, :maxlength => 20, :size => 7 %></td>
    </tr>
    <%= f.fields_for :product_shipping_method do |shipping_method| %>
    <tr>
      <th><%= shipping_method.label :shipping_method %></th>
      <td><%= shipping_method.collection_select :shipping_method_id, ShippingMethod.all, :id, :presentation, {:prompt => "všechny"}, {:style => "width: 180px;"} %></td>
    </tr>
    <% end %>
    <tr>
      <th><%= f.label :on_sale_from %></th>
      <td><%= f.text_field :on_sale_from %></td>
      <th><%= f.label :on_sale_to %></th>
      <td><%= f.text_field :on_sale_to %></td>
    </tr>
    <tr>
      <th><%= f.label :description_html %></th>
      <td colspan="3"><%= f.cktext_area :description_html, :width => "550px", :height => "200px" %></td>
    </tr>
    <tr>
      <th><%= f.label :description_detail_html %></th>
      <td colspan="3"><%= f.cktext_area :description_detail_html, :width => "550px", :height => "300px" %></td>
    </tr>
    <tr>
      <td colspan="3"><%= f.submit %></td>
    </tr>
  </tbody>
</table>

  <% unless @product.new_record? %>
    <%= render "comments", :f => f %>
  <% end %>
<% end %>

<script type="text/javascript">
  $(document).ready(function() {
      $('#product_on_sale_to').datePicker({clickInput:true});
      $('#product_on_sale_from').datePicker({clickInput:true});

      if(!$('#product_on_sale_from').val()) {
        $('#product_on_sale_to').dpSetStartDate('<%= Date.tomorrow.strftime('%d. %m. %Y') %>');
      } else {
        var date_array = $('#product_on_sale_from').val().split(".");
        $('#product_on_sale_to').dpSetStartDate(new Date(date_array[2], date_array[1], date_array[0]).addDays(1).asString());
      }

      $('#product_on_sale_from').bind('dpClosed', function(e, selectedDates) {
        if(selectedDates) {
          var d = selectedDates[0];
          if(d) {
            date = new Date(d).addDays(1).asString();
            $('#product_on_sale_to').dpSetStartDate(date);

            if($('#product_on_sale_to')) {
              var on_sale_from_date_array = d.asString().split('.');
              var on_sale_to_date_array   = $('#product_on_sale_to').val().split('.');
              var on_sale_from_date = new Date(on_sale_from_date_array[2], on_sale_from_date_array[1], on_sale_from_date_array[0]);
              var on_sale_to_date   = new Date(on_sale_to_date_array[2], on_sale_to_date_array[1], on_sale_to_date_array[0]);
              if((on_sale_from_date > on_sale_to_date)) {
                $('#product_on_sale_to').val(date).trigger("change");
              }
            }
          }
        }
      });

      $('#product_on_sale_from').trigger('dpClosed');
      $('#product_on_sale_to').trigger('dpClosed');
  });
</script>